const config = require('../config.js');
var pub_url =config.pub_url;
var url = config.url;

function request(postData, doSuccess, doFail, doComplete, methods) {
  wx.showLoading({
    title: '加载中，请稍后',
    mask: true
  })
  let token = wx.getStorageSync('token');
  postData.ds = "default";
  postData.token = token;
  postData.source = "yituiceshi";
  postData.paras.push({ name: 'isall', value: wx.getStorageSync('isall') || 1 })
  postData.paras.push({ name: "current_company", value: wx.getStorageSync('company_key') })
  wx.request({
    url: url,
    data: postData,
    method: methods || 'POST',
    header: {
      "Content-Type": "application/json"
    },
    success: function (res) {
      typeof doSuccess == 'function' && doSuccess(res);
    },
    fail: function () {
      setTimeout(function () {
        wx.showToast({
          title: '网络异常,\r\n请检查网络是否正常连接！',
        })
      }, 1000)
      typeof doFail == 'function' && doFail()
    },
    complete: function (res) {
      typeof doComplete == 'function' && doComplete()
      setTimeout(function () {
        wx.hideLoading();
      }, 1500);
    }
  });
};

function pub_request(postData, doSuccess, doFail, doComplete, methods) {
  wx.showLoading({
    title: '加载中，请稍后',
    mask: true
  })
  postData.ds = "default";
  postData.source = "ei";
  wx.request({
    url: pub_url,
    data: postData,
    method: methods || 'POST',
    header: {
      "Content-Type": "application/json"
    },
    success: function (res) {
      typeof doSuccess == 'function' && doSuccess(res);
    },
    fail: function (res) {
      console.log(res,"res")
      setTimeout(function () {
        wx.showToast({
          title: '网络异常,\r\n请检查网络是否正常连接！',
        })
      }, 1000)
      typeof doFail == 'function' && doFail()
    },
    complete: function (res) {
      console.log(res,"com")
      typeof doComplete == 'function' && doComplete()
      setTimeout(function () {
        wx.hideLoading();
      }, 1500);
    }
  });
};

function requesttype(postData, type, doSuccess, doFail, doComplete, methods) {
  let token = wx.getStorageSync('token');
  wx.showLoading({
    title: '加载中，请稍后',
    mask: true
  })
  postData.paras.push({ name: 'isall', value: wx.getStorageSync('isall') || 1 })
  postData.paras.push({ name: "current_company", value: wx.getStorageSync('company_key')})
  postData.ds = "default";
  postData.token = token;
  postData.source = "yituiceshi";
  postData.rtype = type;
  wx.request({
    url: url,
    data: postData,
    method: methods || 'POST',
    header: {
      "Content-Type": "application/json"
    },
    success: function (res) {
      typeof doSuccess == 'function' && doSuccess(res);
    },
    fail: function () {
      setTimeout(function () {
        wx.showToast({
          title: '网络异常,\r\n请检查网络是否正常连接！',
        })
      }, 1000)
      typeof doFail == 'function' && doFail()
    },
    complete: function (res) {
      typeof doComplete == 'function' && doComplete()
      setTimeout(function () {
        wx.hideLoading();
      }, 1500);
    }
  });
};

function send(setUrl, postData, doSuccess, doFail, doComplete, methods) {
  wx.showLoading({
    title: '加载中，请稍后',
    mask: true
  })
  wx.request({
    url: setUrl,
    data: postData,
    method: methods || 'POST',
    header: {
      "Content-Type": "application/json"
    },
    success: function(res) {
      if (typeof doSuccess == "function") {
        doSuccess(res);
      }
    },
    fail: function() {
      setTimeout(function() {
        wx.showToast({
          title: `网络异常,\r\n请检查网络是否正常连接`,
          icon: 'none'
        })
      }, 1000)
      if (typeof doFail == "function") {
        doFail();
      }
    },
    complete: function() {
      if (typeof doComplete == "function") {
        doComplete();
      }
      setTimeout(function() {
        wx.hideLoading();
      }, 1500);
    }
  });
};

module.exports.requesttype = requesttype;
module.exports.request = request;
module.exports.pub_request = pub_request;
module.exports.send = send;
